import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;

public class InGame {
	Monster testmonster, tm2;

	public ArrayList<Block> blocks;
	public ArrayList<Monster> monsters;
	public ArrayList<Character> players;
	public ArrayList gameObjects;
	public ArrayList collisionListCollection;
	
	public Character player1, player2;
	
	private Game observer;

	// 46, 31
	public InGame(Game observer) {
		collisionListCollection = new ArrayList();
		players = new ArrayList<Character>();
		blocks = new ArrayList<Block>();
		monsters = new ArrayList<Monster>();
		gameObjects = new ArrayList();
		this.observer = observer;
		

		gameObjects.add(new Switch(25*16, 16*16));
		gameObjects.add(new Door(240, 29*16, "images/doorOpen.png", "images/doorClosed.png", observer));
		gameObjects.add(new WinBlock((27*16), (18*16)));
		
		initBlocks();
		initMonsters();
		initPlayers();
		initCLC();
	}

	private void initPlayers() {
		
		player1 = new Character(16, 16, "images/player1.gif", collisionListCollection, true);
		player2 = new Character(48, 48, "images/player2.gif", collisionListCollection, false);

		player1.setUpKey(KeyEvent.VK_UP);
		player1.setDownKey(KeyEvent.VK_DOWN);
		player1.setLeftKey(KeyEvent.VK_LEFT);
		player1.setRightKey(KeyEvent.VK_RIGHT);
		player1.setJumpKey(KeyEvent.VK_UP);
		player1.setActionKey(KeyEvent.VK_CONTROL);

		player2.setUpKey(KeyEvent.VK_W);
		player2.setDownKey(KeyEvent.VK_S);
		player2.setLeftKey(KeyEvent.VK_A);
		player2.setRightKey(KeyEvent.VK_D);
		player2.setJumpKey(KeyEvent.VK_W);
		player2.setActionKey(KeyEvent.VK_G);

		players.add(player1);
		players.add(player2);
	}

	private void initBlocks() {
		for (int i = 1; i <= 45; i++) {
			blocks.add(new Block((i * 16), (30 * 16), "images/block.png"));
		}
		blocks.add(new Block((1 * 16), (29 * 16), "images/block.png"));
		blocks.add(new Block((45 * 16), (29 * 16), "images/block.png"));
		

		for(int i=1;i<5;i++){
			newBlock(((26+i)*16), (20*16));
		}
		for(int i=1;i<11;i++){
			newBlock(((15+i)*16), (24*16));
		}
		for(int i=1;i<30;i++){
			newBlock(((26)*16), ((i-1)*16));
		}
		for(int i=1;i<7;i++){
			newBlock((240), ((22+i)*16));
		}
		newBlock((25*16), (17*16));
		newBlock((50*16), (24*16));
	}
	
	private void initMonsters(){
		newMonster((27*16), (24*16));
	}
	
	
	
	private void initCLC(){
		collisionListCollection.add(players);
		collisionListCollection.add(blocks);
		collisionListCollection.add(monsters);
		collisionListCollection.add(gameObjects);
		collisionListCollection.add(players.get(0).getProjectiles());
		collisionListCollection.add(players.get(1).getProjectiles());
	}

	public void draw(Graphics2D g2d, Game observer) {
		for (int i = 0; i < blocks.size(); i++) {
			Block tBlock = (Block) blocks.get(i);
			g2d.drawImage(tBlock.getImage(), tBlock.getX(), tBlock.getY(), observer);
		}

		for (int i = 0; i < monsters.size(); i++) {
			Monster tMonster = (Monster) monsters.get(i);
			tMonster.draw(g2d, observer);
		}

		for (int i = 0; i < players.size(); i++) {
			Character tCharacter = (Character) players.get(i);
			tCharacter.draw(g2d, observer);
			for (int j = 0; j < players.get(i).getProjectiles().size(); j++){
				Projectile tProjectile = (Projectile) players.get(i).getProjectiles().get(j);
				tProjectile.draw(g2d, observer);
			}
		}

		for (int i = 0; i < gameObjects.size(); i++){
			GameObject tGameObject = (GameObject) gameObjects.get(i);
			tGameObject.draw(g2d, observer);
		}
	}

	public void tick() {
		for (int i = 0; i < monsters.size(); i++) {
			Monster tMonster = (Monster) monsters.get(i);
			tMonster.move(blocks);
		}
		for (int i = 0; i < players.size(); i++) {
			if(players.get(i).isLiving()){
				Character tCharacter = (Character) players.get(i);
				tCharacter.move();
				for (int j = 0; j < players.get(i).getProjectiles().size(); j++){
					Projectile tProjectile = (Projectile) players.get(i).getProjectiles().get(j);
					if (tProjectile.exists())
						tProjectile.move();
					else
						players.get(i).getProjectiles().remove(j);
				}
			}
			else{
				players.remove(i);
			}
		}
	}

	public void keyPressed(KeyEvent event) {
		
		for (int i = 0; i < players.size(); i++) {
			Character player = players.get(i);
			if (event.getKeyCode() == player.getUpKey()) {
				//player.setYSpeed(2); 
			}
			if (event.getKeyCode() == player.getDownKey()) {
				player.changeState();
			}
			if (event.getKeyCode() == player.getLeftKey()) {
				player.setXSpeed(-player.getSpeed());
			}
			if (event.getKeyCode() == player.getRightKey()) {
				player.setXSpeed(player.getSpeed());
			}
			if (event.getKeyCode() == player.getJumpKey()) {
				player.jump();
			}
			if (event.getKeyCode() == player.getActionKey()) {
				player.specialAction();
			}
		}
	}

	public void keyReleased(KeyEvent e) {

		for (int i = 0; i < players.size(); i++) {
			Character player = players.get(i);
			if (e.getKeyCode() == player.getUpKey()) {
				//player.setYSpeed(0);
			}
			if (e.getKeyCode() == player.getDownKey()) {
				player.releaseKey(player.getDownKey());
			}
			if (e.getKeyCode() == player.getLeftKey()) {
				player.setXSpeed(0);
			}
			if (e.getKeyCode() == player.getRightKey()) {
				player.setXSpeed(0);
			}
			if (e.getKeyCode() == player.getJumpKey()) {
				player.stopJump();
			}
			if (e.getKeyCode() == player.getActionKey()) {
				player.releaseKey(player.getActionKey());
			}
		}
	}
	public void newBlock(int XPos, int YPos){
		blocks.add(new Block(XPos, YPos, "images/block.png"));
	}
	public void newMonster(int XPos, int YPos){
		monsters.add(new Monster(XPos, YPos, "images/monster.gif", 3));
	}
}
